<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card v-loading="loading">
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="handleAdd"
              >新增角色</el-button
            >
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column
                label="序号"
                width="100"
                type="index"
                :index="indexMethod"
              />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="handleEdit(row.id)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="handleDelete(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- current-page 表示的当前是哪一页 -->
            <!-- page-sizes 动态设置页容量的下拉列表 -->
            <!-- page-size 表示的是当前的页容量 -->
            <!-- layout 表示的分页的展示样式 -->
            <!-- total 表示的 一共有多少条 -->
            <!-- @current-change 表示的点击页码的时候执行的事件-->
            <!-- @size-change 表示的动态设置页容量的时候执行的事件 -->
            <div
              style="margin-top: 10px; display: flex; justify-content: flex-end"
            >
              <el-pagination
                :current-page="page"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
              />
            </div>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <!-- 公司信息 -->
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyForm.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyForm.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyForm.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyForm.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 弹框 -->
      <el-dialog
        :title="showTitle"
        :visible="showDialog"
        @close="closeDialog"
        :close-on-click-modal="false"
      >
        <el-form
          ref="roleForm"
          :model="form"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="confirmAddRole">确认</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getRoleListApi,
  delRoleListApi,
  addRoleListApi,
  getRoleDataApi,
  editRoleApi,
  getCompanyInfoApi
} from '@/api/setting'
import { mapGetters } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      page: 1,
      pagesize: 10,
      roleList: [],
      total: 0,
      loading: false,
      showDialog: false,
      form: {
        name: '',
        id: '',
        description: ''
      },
      rules: {
        name: [
          // required 非空校验
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          { min: 1, max: 10, message: '角色名称长度为1-10', trigger: 'blur' }
        ]
      },
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      }
    }
  },
  computed: {
    ...mapGetters['companyId'],
    showTitle() {
      return this.form.id ? '修改角色' : '添加角色'
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getCompanyInfo() {
      const { data } = await getCompanyInfoApi(this.companyId)
      this.companyForm = data
    },
    /**
     * 数据回显
     */
    async handleEdit(id) {
      this.showDialog = true
      const { data } = await getRoleDataApi(id)
      this.form = data
    },
    /**
     * 修改角色
     */
    // handleEdit() {},
    indexMethod(index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    async getRoleList() {
      this.loading = true
      const { data } = await getRoleListApi(this.page, this.pagesize)
      this.roleList = data.rows
      this.total = data.total
      this.loading = false
    },
    // 弹出弹框
    handleAdd() {
      this.showDialog = true
    },
    closeDialog() {
      this.showDialog = false
      // 清空表单状态
      this.$refs.roleForm.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    /**
     * 点击页码
     */
    handleCurrentChange(val) {
      this.page = val
      this.getRoleList()
    },
    /**
     * 修改页容量
     */
    handleSizeChange(val) {
      this.pagesize = val
      this.getRoleList()
    },
    /**
     * 删除角色
     */
    handleDelete(id) {
      // console.log(data.id)
      this.$confirm('您确定要删除吗？该操作不可逆哟', '温馨提示')
        .then(async() => {
          await delRoleListApi(id)
          this.$message.success('删除成功')
          if (this.roleList.length === 1 && this.page > 1) {
            this.page--
          }
          this.getRoleList()
        })
        .catch(() => {})
    },
    /**
     * 添加角色
     */
    confirmAddRole() {
      this.$refs.roleForm.validate(async(flag) => {
        if (!flag) return
        if (this.form.id) {
          await editRoleApi(this.form)
          this.$message.success('恭喜！修改成功')
        } else {
          await addRoleListApi(this.form)
          this.$message.success('恭喜！添加成功')
        }
        this.getRoleList()
        this.closeDialog()
      })
    }
  }
}
</script>

<style>
</style>
